package com.os.meituan.service.order;

import com.os.meituan.common.XJsonInfo;
import com.os.meituan.model.dtomodel.order.DTO_ORDER_CommerInfo;
import com.os.meituan.model.dtomodel.order.DTO_ORDER_GetOrder;
import com.os.meituan.model.pomodel.Shop.T_SHOP_Good;
import com.os.meituan.model.pomodel.Shop.T_SHOP_Imformation;
import com.os.meituan.model.pomodel.order.T_ORDER_Good;
import com.os.meituan.model.pomodel.order.T_Order;
import com.os.meituan.repository.order.T_ORDER_GetOrderResponsitory;
import com.os.meituan.repository.order.T_ORDER_GoodResponsitory;
import com.os.meituan.repository.Shop.OA_SHOP_InformationResponsitory;
import com.os.meituan.repository.good.T_SHOP_GoodResponsitory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderService {
    @Resource
    private T_SHOP_GoodResponsitory t_shop_goodResponsitory;
    @Resource
    private T_ORDER_GetOrderResponsitory t_order_getOrderResponsitory;
    @Resource
    private OA_SHOP_InformationResponsitory oa_shop_informationResponsitory;
    @Resource
    private T_ORDER_GoodResponsitory t_order_goodResponsitory;

    public XJsonInfo totalPrice(DTO_ORDER_GetOrder reObj) {
        // 求出订单总价
        var totalPrice = getOrderTotalPrice(reObj.getDtoOrderCommerinfos());
        return new XJsonInfo().setData(totalPrice);
        //验证订单是否合理
    }

    private Double getOrderTotalPrice(List<DTO_ORDER_CommerInfo> goodList) {
        var totalPrices = 0.0D;


        for (int i = 0; i < goodList.size(); i++) {
            if (goodList.get(i).getId() != null) {
                var good = t_shop_goodResponsitory.findById(goodList.get(i).getId()).orElse(null);
                var poShopGoodInfo = new T_SHOP_Good();
                poShopGoodInfo.setDiscountPrice(good.getDiscountPrice());
                poShopGoodInfo.setGoodCount(good.getGoodCount());
                var totalPrice = poShopGoodInfo.getGoodCount() * poShopGoodInfo.getDiscountPrice();
                totalPrices = totalPrices + totalPrice;


            }else {
                throw new RuntimeException("订单商品不能为空");
            }

        }
        return totalPrices;
    }

    public XJsonInfo saveOrder(DTO_ORDER_GetOrder reObj){

      /*  for(int i=0; i<reObj.getDtoOrderCommerinfos().size();i++){

                var poShopGoodInfo = new T_SHOP_Good();
                poShopGoodInfo.setDiscountPrice(reObj.getDtoOrderCommerinfos().get(i).getDiscountPrice());
                poShopGoodInfo.setGoodCount(reObj.getDtoOrderCommerinfos().get(i).getGoodCount());
                poShopGoodInfo.setName(reObj.getDtoOrderCommerinfos().get(i).getCommerName());
                t_shop_goodResponsitory.save(poShopGoodInfo);
            }*/

        var poShopImformation=new T_SHOP_Imformation();
        poShopImformation.setId(reObj.getDtoOrderShopInfo().getId());
        poShopImformation.setStoreName(reObj.getDtoOrderShopInfo().getStoreName());
        poShopImformation.setStorePhoneNum(reObj.getDtoOrderShopInfo().getStorePhoneNum());
        poShopImformation.setStoreAddress(reObj.getDtoOrderShopInfo().getStoreAddress());
        poShopImformation.setLatitude(reObj.getDtoOrderShopInfo().getLatitude());
        poShopImformation.setLongitude(reObj.getDtoOrderShopInfo().getLongitude());
        oa_shop_informationResponsitory.save(poShopImformation);
        var poOrder= new T_Order();
        var totalPrices=this.getOrderTotalPrice(reObj.getDtoOrderCommerinfos());
        var poOrderGood=new T_ORDER_Good();

        poOrderGood.setName(reObj.getDto_order_goodinfo().getName());
        poOrderGood.setGoodNum(reObj.getDto_order_goodinfo().getGoodNum());
        poOrderGood.setGoodId(reObj.getDto_order_goodinfo().getGoodId());
        poOrderGood.setOrderId(reObj.getDto_order_goodinfo().getOrderId());
        t_order_goodResponsitory.save(poOrderGood);
      /*  if(reObj.getDtoOrderShopInfo().getId() !=null) {
            var totalPrices=0.0D;
            var shopGood = t_shop_goodResponsitory.findById(reObj.getDtoOrderShopInfo().getId()).orElse(null);
            for (int i = 0; i < reObj.getDtoOrderCommerinfos().size(); i++) {
                if (reObj.getDtoOrderCommerinfos().get(i).getId() != null) {
                    var good = t_shop_goodResponsitory.findById(reObj.getDtoOrderCommerinfos().get(i).getId()).orElse(null);
                    var poShopGoodInfo = new T_SHOP_Good();
                    poShopGoodInfo.setDiscountPrice(good.getDiscountPrice());
                    poShopGoodInfo.setGoodCount(good.getGoodCount());
                    var totalPrice = poShopGoodInfo.getGoodCount() * poShopGoodInfo.getDiscountPrice();
                    totalPrices = totalPrices + totalPrice;

                } else {
                    throw new RuntimeException("订单商品不能为空");
                }
            }*/
            poOrder.setPrice(totalPrices);
            poOrder.setAddress(reObj.getOrderInfo().getAddress());
            poOrder.setCreateTime(reObj.getOrderInfo().getCreateTime());
            poOrder.setDeliveryTime(reObj.getOrderInfo().getDeliveryTime());
            poOrder.setSignTime(reObj.getOrderInfo().getSignTime());
            poOrder.setOrderStatus(reObj.getOrderInfo().getOrderStatus());
            poOrder.setShopId(reObj.getOrderInfo().getShopId());
            t_order_getOrderResponsitory.save(poOrder);

        return new XJsonInfo();
    }
}